/**
 * 作者: 会写BUG
 * 创作日期: 2025/3/21 10:22
 * 描述: cwbQtPractice.NumberChess
 *
 **/
#include "number_chess.h"

#include <QPainter>

NumberChess::NumberChess(QWidget* _parent, const QRect & _rect)
    : QWidget(_parent)
{
    // int random_value = QRandomGenerator::global()->bounded(100);
    // number_ = random_value > 90 ? 4 : 2;
    setGeometry(_rect);
    initChess();

    
}

NumberChess::~NumberChess()
{
}

void NumberChess::setNeedAdd(bool _need_add)
{
    need_add_ = _need_add;
}

void NumberChess::setNumber(int _number) {
    number_ = _number;
    update(rect());
}

bool NumberChess::needAdd() const
{
    return need_add_;
}

int NumberChess::number() const
{
    return number_;
}

void NumberChess::setIndex(int _index)
{
    index_ = _index;
}

int NumberChess::index() const
{
    return index_;
}

QPropertyAnimation* NumberChess::moveTo(const QRect & _rect)
{
    QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
    animation->setDuration(100);
    animation->setStartValue(geometry());
    animation->setEndValue(_rect);
    return animation;
}

bool NumberChess::needDelete() const
{
    return need_delete_;
}

void NumberChess::setNeedDelete(bool _need_delete)
{
    need_delete_ = _need_delete;
}

void NumberChess::initChess()
{
    color_map_[2] = QColor(255, 230, 148);
    color_map_[4] = QColor(255, 230, 104);
    color_map_[8] = QColor(255, 212, 116);
    color_map_[16] = QColor(255, 190, 32);
    color_map_[32] = QColor(251, 182, 91);
    color_map_[64] = QColor(250, 160, 70);
    color_map_[128] = QColor(240, 140, 50);
    color_map_[256] = QColor(230, 120, 40);
    color_map_[512] = QColor(220, 100, 30);
    color_map_[1024] = QColor(210, 80, 20);
    color_map_[2048] = QColor(200, 60, 10);
    color_map_[4096] = QColor(190, 40, 0);
    color_map_[8192] = QColor(180, 20, 0);
    color_map_[16384] = QColor(170, 0, 0);
    color_map_[32768] = QColor(160, 0, 0);
    color_map_[65536] = QColor(150, 0, 0);

    QFont font("Arial", 24, QFont::Bold);
    this->setFont(font);
}

void NumberChess::paintEvent(QPaintEvent* event)
{
    int shadow_offset = 4;

    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.setPen(Qt::transparent);

    painter.setBrush(QColor(171, 155, 129));
    painter.drawRect(shadow_offset, shadow_offset, width() - shadow_offset, height() - shadow_offset);

    painter.setBrush(color_map_.value(number_));
    painter.drawRect(0, 0, width() - shadow_offset, height() - shadow_offset);

    /* 居中绘制文字 number_ */
    painter.setPen(Qt::white);
    painter.drawText(0, 0, width(), height(),
        Qt::AlignCenter, QString("%1").arg(number_));
}



